package com.linkstec.json;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonLoop {
	private static final Logger logger = LogManager.getLogger();
	 
    public static String json = "{\"TITLE\":13,\"FORM\":{\"USERNAME\":\"Rick and Morty\"},\"ARRAY\":[{\"FIRST\":\"Rick\"},{\"LAST\":\"Morty\"}]}";
 
    public static void jsonLoop(Object object) {
 
        if(object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
                Object o = entry.getValue();
                if(o instanceof String) {
                	logger.info("key:" + entry.getKey() + "，value:" + entry.getValue());
                } else {
                    jsonLoop(o);
                }
            }
        }
        if(object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            for(int i = 0; i < jsonArray.size(); i ++) {
                jsonLoop(jsonArray.get(i));
            }
        }
    }
 
    public static void main(String[] args) {
 
        JSONObject jsonObject = JSON.parseObject(json);
        logger.info(jsonObject);
        jsonLoop(jsonObject);
    }
}